package com.cloudoer.blog.advice;

import com.cloudoer.blog.base.BaseController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 错误控制器，一旦controller发生错误，都会经过这个
 */
@RestControllerAdvice
public class HarshingExceptionHandler extends BaseController {
	
	@ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest reqest,
                               HttpServletResponse response, Exception e) throws Exception {
    	e.printStackTrace();

    	//if (isAjax(reqest)) {
    		return result(ERROR, e.getMessage());
//    	} else {
//    		ModelAndView mav = new ModelAndView();
//            mav.addObject("exception", e);
//            mav.addObject("url", reqest.getRequestURL());
//            mav.setViewName(IMOOC_ERROR_VIEW);
//            return mav;
//    	}
    }
	
	/**
	 * 
	 * @Title: IMoocExceptionHandler.java
	 * @Package com.imooc.exception
	 * @Description: 判断是否是ajax请求
	 * Copyright: Copyright (c) 2017
	 * Company:FURUIBOKE.SCIENCE.AND.TECHNOLOGY
	 * 
	 * @author leechenxiang
	 * @date 2017年12月3日 下午1:40:39
	 * @version V1.0
	 */
	public static boolean isAjax(HttpServletRequest httpRequest){
		return  (httpRequest.getHeader("X-Requested-With") != null  
					&& "XMLHttpRequest"
						.equals( httpRequest.getHeader("X-Requested-With").toString()) );
	}
}
